package com.cybertron.ironhide.manage.service.impl;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Service;

import com.cybertron.ironhide.manage.common.Constants;
import com.cybertron.ironhide.manage.common.base.Pager;
import com.cybertron.ironhide.manage.domain.Product;
import com.cybertron.ironhide.manage.persistence.ProductMapper;
import com.cybertron.ironhide.manage.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

	@Resource
	private ProductMapper productMapper;

	@Resource
	private SolrServer solrServer;

	@Override
	public void insertSelective(Product record) {
		productMapper.insertSelective(record);
		if (record.getGroupId() != null) {
			productMapper.updateGroupIdById(record.getGroupId());
		}
		updateIndex(record);
	}

	@Override
	public void updateByPrimaryKeySelective(Product record) {
		productMapper.updateByPrimaryKeySelective(record);
		if (record.getGroupId() != null) {
			productMapper.updateGroupIdById(record.getGroupId());
		}
		updateIndex(record);
	}

	@Override
	public Product selectByTargetUrl(String targetUrl) {
		return productMapper.selectByTargetUrl(targetUrl);
	}

	@Override
	public List<Product> selectBySelective(Product product, Pager pager) {
		return productMapper.selectBySelective(product, pager);
	}

	@Override
	public void deleteIndex() {
		try {
			solrServer.deleteByQuery("*:*");
			solrServer.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Product selectByPrimaryKey(String id) {
		return productMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateIndex() {
		List<String> seedIds = productMapper
				.selectSeedIdByMax(Constants.SEED_MAX_COUNT);
		for (String seedId : seedIds) {
			productMapper.deleteBySeedIdAndMaxCount(seedId,
					Constants.SEED_MAX_COUNT);
		}
		SolrQuery query = new SolrQuery();
		query.set("qt", "/dataimport");
		query.add("clean", "true");
		query.add("commit", "true");
		query.add("indent", "true");
		query.add("entity", "product");
		query.add("optimize", "false");
		query.add("command", "full-import");
		try {
			solrServer.query(query);
		} catch (SolrServerException e) {
			e.printStackTrace();
		}

	}

	public void updateIndex(Product product) {
		SolrInputDocument document = new SolrInputDocument();
		if (product != null && product.getGroupId() != null) {
			Product groupProduct = productMapper.selectByPrimaryKey(product
					.getGroupId());
			if (groupProduct != null) {
				product = groupProduct;
			}
		}
		document.addField("id", product.getId());
		document.addField("targetUrl", product.getTargetUrl());
		document.addField("title", product.getTitle());
		document.addField("source", product.getSource());
		document.addField("image", product.getImage());
		document.addField("price", product.getPrice());
		document.addField("listPrice", product.getListPrice());
		document.addField("lowestPrice", product.getLowestPrice());
		document.addField("highestPrice", product.getHighestPrice());
		document.addField("freeShipping", product.getFreeShipping());
		document.addField("summary", product.getSummary());
		document.addField("keywords", product.getKeywords());
		document.addField("description", product.getDescription());
		document.addField("categoryId", product.getCategoryId());
		document.addField("groupId", product.getGroupId());
		try {
			UpdateResponse response = solrServer.add(document);
			solrServer.commit();
			response.getStatus();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteByPrimaryKey(String id) {
		productMapper.deleteByPrimaryKey(id);
		try {
			solrServer.deleteById(id);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
